package com.campusrecruitment.controller;

import com.campusrecruitment.common.JsonUtils;
import com.campusrecruitment.common.R;

import com.campusrecruitment.pojo.employer;
import com.campusrecruitment.pojo.loginUser;
import com.campusrecruitment.pojo.student;
import com.campusrecruitment.service.loginUserService;
import com.fasterxml.jackson.core.JsonProcessingException;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.List;
import java.util.Random;

@Slf4j
@RestController
@RequestMapping("/loginUser")
public class loginUserController {
    private ObjectMapper mapper = new ObjectMapper();

    /**
     * 学生登录
     * @param loginUser
     * @return
     */
    @PostMapping("/studentLogin")
    public R<loginUser>studentLogin(@RequestBody loginUser loginUser, HttpServletRequest request) throws JsonProcessingException {
        //初始化json工具和users
        JsonUtils jsonUtils=new JsonUtils(new File("D:\\IDEA\\CampusRecruitment\\src\\main\\resources\\Data\\user.json"));
        String jsonList=jsonUtils.readJson(String.class);
        List<loginUser> users= mapper.readValue(jsonList,new TypeReference<List<loginUser>>(){});

        //获取前端发送的用户名和密码
        String name=loginUser.getName();
        String userName=loginUser.getUsername();
        String password =loginUser.getPassword();

        loginUser loginuser=new loginUser();
//根据页面提交的用户名username查询users数据
        for (loginUser user :users
             ) {
            if ((user.getUsername().equals(userName)) && (user.getName().equals(name))){
                loginuser=user;
                break;
            }
        }
//3、如果没有查询到则返回登录失败结果
        if( loginuser.getUsername() == null)
            return R.error("用户不存在");

//4、密码比对，如果不一致则返回登录失败结果
        if(! loginuser.getPassword().equals(password))
            return R.error("密码错误");

        //6、登录成功，将学生姓名存入Session并返回登录成功结果
        request.getSession().setAttribute("loginUser", name);

        return  R.success(loginUser);
    }

    /**
     * 单位登录
     * @param loginUser
     * @return
     */
    @PostMapping("/employerLogin")
    public R<loginUser>employerLogin(@RequestBody loginUser loginUser,  HttpServletRequest request) throws JsonProcessingException {
        //初始化json工具和users
        JsonUtils jsonUtils=new JsonUtils(new File("D:\\IDEA\\CampusRecruitment\\src\\main\\resources\\Data\\user.json"));
        String jsonList=jsonUtils.readJson(String.class);
        List<loginUser> users= mapper.readValue(jsonList,new TypeReference<List<loginUser>>(){});

        //获取前端发送的用户名和密码
        String name=loginUser.getName();
        String userName=loginUser.getUsername();
        String password =loginUser.getPassword();

        loginUser loginuser=new loginUser();
//根据页面提交的用户名username查询users数据
        for (loginUser user :users
        ) {
            if (user.getUsername().equals(userName)&&user.getName().equals(name)){
                loginuser=user;
            break;
            }
        }
//3、如果没有查询到则返回登录失败结果
        if( loginuser.getUsername() == null)
            return R.error("用户不存在");

//4、密码比对，如果不一致则返回登录失败结果
        if(! loginuser.getPassword().equals(password))
            return R.error("密码错误");

        //6、登录成功，将员工姓名存入Session并返回登录成功结果
        request.getSession().setAttribute("loginUser", name);

        return  R.success(loginUser);
    }

    /**
     * 学生注册
     * @param loginUser
     * @return
     */
    @PostMapping("/studentSignUp")
    public R<loginUser>studentSignin(@RequestBody loginUser loginUser, HttpServletRequest request) throws JsonProcessingException {
        //获取数据进行存入
        String name=loginUser.getName();
        String userName =loginUser.getUsername();
        String password=loginUser.getPassword();

        //初始化json工具和users
        JsonUtils jsonUtils=new JsonUtils(new File("D:\\IDEA\\CampusRecruitment\\src\\main\\resources\\Data\\user.json"));
        String jsonList=jsonUtils.readJson(String.class);
        List<loginUser> users= mapper.readValue(jsonList,new TypeReference<List<loginUser>>(){});

        long min = 1000;
        long max = 1000000;
        long rangeLong = min + (((long) (new Random().nextDouble() * (max - min))));
        //存入学生用户数据
        loginUser user=new loginUser();
        user.setName(name);
        user.setUsername(userName);
        user.setPassword(password);
        user.setId(rangeLong);
        user.setType("student");

            users.add(user);

            String jsonString = mapper.writerWithDefaultPrettyPrinter()
                    .writeValueAsString(users);
            jsonUtils.writeJson(jsonString);

        //同时存入学生表
        //初始化json工具
        JsonUtils jsonUtils2=new JsonUtils(new File("D:\\IDEA\\CampusRecruitment\\src\\main\\resources\\Data\\student.json"));
        String jsonList2=jsonUtils2.readJson(String.class);
        //得到学生信息集合
        List<student> students= mapper.readValue(jsonList2,new TypeReference<List<student>>(){});
        student s=new student();
        s.setName(name);
        students.add(s);
        String jsonString2 = mapper.writerWithDefaultPrettyPrinter()
                .writeValueAsString(students);
        jsonUtils2.writeJson(jsonString2);

        //6、注册成功，将用户姓名存入Session并返回登录成功结果
        request.getSession().setAttribute("loginUser", name);

        return R.success(loginUser);
    }

    /**
     * 单位注册
     * @param loginUser
     * @return
     */
    @PostMapping("/employerSignUp")
    public R<loginUser> employerSignin(@RequestBody loginUser loginUser, HttpServletRequest request) throws JsonProcessingException {
        //获取数据进行存入
        String name=loginUser.getName();
        String userName =loginUser.getUsername();
        String password=loginUser.getPassword();

        //初始化json工具和users
        JsonUtils jsonUtils=new JsonUtils(new File("D:\\IDEA\\CampusRecruitment\\src\\main\\resources\\Data\\user.json"));
        String jsonList=jsonUtils.readJson(String.class);
        List<loginUser> users= mapper.readValue(jsonList,new TypeReference<List<loginUser>>(){});

        long min = 1000;
        long max = 1000000;
        long rangeLong = min + (((long) (new Random().nextDouble() * (max - min))));
        //存入学生用户数据
        loginUser user=new loginUser();
        user.setName(name);
        user.setUsername(userName);
        user.setPassword(password);
        user.setId(rangeLong);
        user.setType("student");

        users.add(user);

        String jsonString = mapper.writerWithDefaultPrettyPrinter()
                .writeValueAsString(users);
        jsonUtils.writeJson(jsonString);


        //同时存入单位表
        //初始化json工具
        JsonUtils jsonUtils2=new JsonUtils(new File("D:\\IDEA\\CampusRecruitment\\src\\main\\resources\\Data\\employer.json"));
        String jsonList2=jsonUtils2.readJson(String.class);
        //得到单位信息集合
        List<employer> employers= mapper.readValue(jsonList2,new TypeReference<List<employer>>(){});
        employer e=new employer();
        e.setName(name);
        employers.add(e);
        String jsonString2 = mapper.writerWithDefaultPrettyPrinter()
                .writeValueAsString(employers);
        jsonUtils2.writeJson(jsonString2);
        //6、登录成功，将用户姓名存入Session并返回登录成功结果
        request.getSession().setAttribute("loginUser", name);

        return R.success(loginUser);
   }
}
